public class Trailer implements Loadable{
	protected Trailer towBarFront;
	protected Trailer towBarBack;
	private final BigAnimal[] bigAnimals;
	protected final Compartment compartment;
	
	public Trailer(BigAnimal[] bigAnimals, Compartment compartment) {
		this.towBarFront = null;
		this.towBarBack = null;
		this.bigAnimals = bigAnimals;
		this.compartment = compartment;
	}
	
	public boolean load(Loadable loadable) {
			return loadable.loadByTrailer(this);
	}
	
	public Animal unLoad() {
		this.towBarFront.towBarBack = this.towBarBack;
		if (towBarBack != null) {
			towBarBack.towBarFront = this.towBarFront;
		}
		return compartment.unload();
	}
	
	public boolean loadByTrailer(Trailer trailer) {
		if (trailer.towBarBack == null) {
			trailer.towBarBack = this;
			this.towBarFront = trailer;
			return true;
		}
		return false;
	}
	
	public boolean loadByTransporter(Transporter transporter) {
		if (transporter.towBarBack == null) {
			transporter.towBarBack = this;
			this.towBarFront = transporter;
			return true;
		}
		return false;
	}
	
	public boolean isEmpty() {
		return (towBarBack == null);
	}
	
	public Compartment getCompartment() {
		return compartment;
	}
	
	public String list() {
		String output ="";
		Trailer temp = this;
		if(temp==null) {
			return "";
		}
		do{
			
			output+="inside:\n";
			if(temp.getCompartment().getAnimal()==null) {
				output+=" nothing\n";
			} else {
				output+=" "+temp.getCompartment().getAnimal().toString()+'\n';
			}
			
			if(temp.bigAnimals!=null) {
				output+="outside:\n";
				for(int i = 0;i < temp.bigAnimals.length;i++) {
					output+=" "+temp.bigAnimals[i].toString()+'\n';
				}
			} else {
				output+="outside:\n nothing\n";
			}
			temp = temp.towBarBack;
		}while(temp!=null);
		return output;
	}
		
}